import shapefile

# r为读取原始的.shp文件； w为将要写入的新的.shp文件
r = shapefile.Reader("shapefiles/ep202009.026_5day_pgn.shp")
w = shapefile.Writer("shapefiles/ep202009")

# .shp 原始文件的信息（记录和图形）：
# >>> print(r)
# shapefile Reader
#     1 shapes (type 'POLYGON')
#     1 records (8 fields)
# >>> print(r.record())
# Record #0: ['RICK', 'TD', '091021/2100', '26', 20, 72, 'ep']
"""------------------------------------------------------------------"""

# 1. 复制（记录）字段
w.fields = list(r.fields)
# 2.复制记录（属性）
for itme in r.iterShapeRecords():
    w.record(*itme.record)

# 3. 复制图形信息
for s in r.iterShapes():
    w.poly([s.points])


# 4. 添加新的记录和图形
w.record("STANLEY", "TD", "091022/1500", "27", "21", "48", "ep")
w.poly([[[-104, 24], [-104, 25], [-103, 25], [-104, 24]]])
w.close()
r.close()

# 5. 查看新的.shp文件的内容其中 0是复制上一个文件的信息，1是新添加的信息
r = shapefile.Reader("shapefiles/ep202009")
print(r)
print(r.record(0))
print(r.record(1))
print(r.shape(0).points)
print(r.shape(1).points)
r.close()
